<template>
  <transition name="fade">
    <div class="wrap">
      <search-input></search-input>
      <div class="swiper_wrap">
        <div class="img_wrap" v-if="slider_data.length">
          <slider :isdos="true" :autoplay="true" :scroll_type="'slide'">
            <div v-for="(item, index) in slider_data" class="swiper-slide">
              <img v-lazy="item.pic_url" alt="" @click="toClass" :data-cid="1">
            </div>
          </slider>
        </div>
      </div>
      <div class="xian_buy_wrap" v-if="xian_data.length">
        <div class="xian_icon_wrap">
          <img src="../assets/img/xianshi.png" alt="">
          <div>
            <p>距离结束还剩 </p>
            <div v-if="m">
              <span v-show="d>0">{{d}} 天</span>
              <b>{{h}}</b>
              <b>{{m}}</b>
              <b>{{s}}</b>
            </div>
          </div>
        </div>
        <div class="xian_buy_body">
          <ul>
            <li @click.prevent="selectItem(item.goodsId)" v-for="(item , index) in xian_data">
              <div class="xian_img_wrap">
                <img v-lazy="item.image"  alt="">
              </div>
              <p>{{item.name}} </p>
              <div class="xian_price_wrap">
                <p>¥{{item.salesPrice}}</p>
                <s>¥{{item.price}}</s>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="hot_buy_wrap">
        <div class="hot_buy" v-for="(item , index) in hot_data">
          <div class="hot_buy_head">
            <div class="hot_icon_wrap">
              <p><img :src="item.icon" alt=""> <b>{{item.title}}</b></p>
            </div>
            <div class="hot_img_wrap">
              <img v-lazy="item.bgimg_url" alt="" @click.prevent>
            </div>

          </div>
          <div class="hot_buy_body">
            <ul>
              <li @click.prevent="selectItem(item.pid)" v-for="(item , index) in item.activity_list">
                <div>
                  <img v-lazy="item.pro_url" alt="">
                </div>
                <p>{{item.tilte}}</p>
                <b>¥ {{item.price}}</b>
              </li>
            </ul>
          </div>
        </div>
      </div>
        <router-view></router-view>
    </div>
  </transition>
</template>

<script type="text/ecmascript-6">
  import slider from 'base/slider'
  import searchInput from 'components/searchInput'
  import {getOffsetTop} from 'assets/dom/dom'
  import * as recommend from 'api/recommend'
  import {fill_zero} from 'assets/base/base'
  import {mapGetters, mapMutations} from 'vuex'

  export default {
    data() {
      return {
        slider_data: [],
        search_val: "",
        hot_data: [],
        xian_data:[],
        is_focus:false,
        d:"",
        h:"",
        m:"",
        s:"",
      }
    },
    mounted() {
    },
    created() {
      recommend.get_slider_list({}).then((res) => {
        if (res.status === 200) {
          let _res = res.data
          if (_res.resStatus === 0) {
            this.slider_data = _res.data
          }
        }
      }, (res) => {
      })

      recommend.get_hot_list({}).then((res) => {

        if (res.status === 200) {
          let _res = res.data
          if (_res.resStatus === 0) {
            this.hot_data = _res.data;
          }
        }
      })

      recommend.get_xian_list({}).then((res)=>{
        console.log(res)
        if (res.status === 200) {
          let _res = res.data;
          if (_res.resStatus === 0) {
            this.xian_data = _res.data
            this.daojishi()
          }else{
            this.xian_data =[]
          }
        }
      })
    },
    activated() {
      console.log(this.searchVal)
      this.search_val = this.searchVal
    },
    computed: {
      ...mapGetters([
        'searchVal'
      ])
    },
    methods: {
      get_focus(){
        this.is_focus=true
        this.$nextTick(function () {
          this.$refs.input.focus()
        })

      },
      toClass(e) {
        const cid = e.target.getAttribute("data-cid")
        this.$router.push({
          path: "/allClass",
          query: {
            class1: "",
            class2: 75,
            class3: "",
          }
        })
      },
      toSearch() {
        if (this.searchVal === "") {
          this.setSearchVal("津猫")
        }
        this.$router.push({
          path: "/search",
        })
      },
      selectItem(id) {
        this.$router.push({
          path: "index/detial",
          query: {
            itemId: id
          }
        })
      },
      daojishi(){
        this.timer=setInterval(()=>{
          let time_num=this.xian_data[0].end_time-new Date().getTime()/1000
          if(time_num<0){
            clearInterval(this.timer)
            this.xian_data=[];
          }
          this.d=Math.floor(time_num/(60*60*24))
          this.h=fill_zero(Math.floor(time_num%(60*60*24)/(60*60)),2);
          this.m=fill_zero(Math.floor(time_num%(60*60)/(60)),2);
          this.s=fill_zero(Math.floor(time_num%(60*60)%(60)),2);
        },1000)

      },
      ...mapMutations({
        setSearchVal: 'SET_SEARCH_VAL'
      })
    },
    components: {
      slider,
      searchInput
    },
    watch: {
      search_val(newVal) {
        this.setSearchVal(newVal)
        console.log(this.searchVal)
      }
    }
  }
</script>

<style scoped lang="scss">
  @import "../assets/sass/mixin";

  .wrap {
    max-width: 768px;
    min-width: 320px;
    width: 100%;
    z-index: 2;
    background: #fff;
    padding-bottom: 46px;
  }

  .swiper_wrap {
    margin-top: 46px;
    width: 100%;
    padding-top: 41.5%;
    position: relative;
  }

  .img_wrap {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
  }

  .xian_buy_wrap {
    margin-top: 5px;
    margin-bottom: 45px;
  }
  .xian_icon_wrap {
      overflow: hidden;
      > img {
        width: 91px;
        height: 34px;
        float: left;
        margin-left: 10px;
      }
      > div {
        margin-top: 5px;
        float: left;
        width:200px;
        margin-left: 3%;
        height: 25px;
        line-height: 25px;
        font-weight: bold;
        background-color: #900403;
        color: #eee;
        padding-left: 14px;
        border-radius: 11px;
        > p {
         float: left;
          letter-spacing: 1px;
        }
        > div {
          > b {
            padding: 2px;
            background-color: #412824;
          }
        }
      }
    }

  .xian_buy_body {
    margin-top: 6px;
    margin-bottom: -20px;
    overflow: hidden;
    > ul {
      font-size: 0;
      overflow: auto;
      white-space: nowrap;
      padding-bottom: 35px;
      margin-bottom: -33px;
      -webkit-overflow-scrolling: touch;
      text-align: left;
      > li {
        vertical-align: top;
        display: inline-block;
       width: 33.3%;
        @media screen and (max-width: 350px) {
          width: 50%;
        }
        font-size: 12px;
        padding-bottom: 2px;
        > .xian_img_wrap{
          width: 70%;
          margin: 0 auto;
          position: relative;
          padding-top: 70%;
          > img {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
          }
        }
        > p {
          width: 84%;
          height: 25px;
          line-height: 13px;
          text-align: left;
          margin: 10px auto 2px auto;
          @include text-ellipsis(2);
          white-space: normal;
        }
        >.xian_price_wrap{
          width: 90%;
          margin: 0 auto;
          overflow: hidden;
          height: 21px;
          >p:nth-child(1){
            float: left;
            width: 50%;
            /* padding-top: 50%; */
            height: 21px;
            color: #fff;
            line-height: 21px;
            text-align: center;
            background-color: #900403;
          }
          >s:nth-child(2){
            line-height: 19px;
            border: 2px solid #900403;
            float: left;
            width: 50%;
            height: 21px;
            color: #999;
            text-align: center;
          }
        }
      }
    }
  }

  .hot_buy {
    width: 94%;
    margin: 0 auto;
    text-align: left;
    margin-bottom: 50px;
    > .hot_buy_head {
      margin-bottom: 10px;
      > .hot_img_wrap {
        width: 100%;
        padding-top: 41.2%;
        position: relative;
        overflow: hidden;
        > img {
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
          height: 100%;
        }
      }

      > .hot_icon_wrap {
        height: 2px;
        background: #25abea;
        width: 188px;
        margin: 23px auto;
        position: relative;
        border-radius: 1px;
        > p {
          position: absolute;
          height: 25px;
          top: -10px;
          width: 116px;
          background: #fff;
          left: 50%;
          transform: translateX(-50%);
          padding-left: 10px;
          padding-right: 10px;
          > img {
            display: inline-block;
            width: 25px;
            height: 25px;
            vertical-align: top;
          }
          > b {
            vertical-align: top;
            font-size: 16px;
            color: #25abea;
            display: inline-block;
            margin-top: 5px;
            margin-left: 2px;
          }
        }
      }
    }
    > .hot_buy_body {
      margin-bottom: -20px;
      overflow: hidden;
      > ul {
        font-size: 0;
        overflow: auto;
        white-space: nowrap;
        padding-bottom: 35px;
        margin-bottom: -33px;
        -webkit-overflow-scrolling: touch;
        > li {
          vertical-align: top;
          display: inline-block;
          width: 33.3%;
          font-size: 12px;
          padding-bottom: 2px;
          > div {
            width: 70%;
            margin: 0 auto;
            position: relative;
            padding-top: 70%;
            > img {
              position: absolute;
              width: 100%;
              height: 100%;
              top: 0;
              left: 0;
            }
          }
          > p {
            width: 84%;
            height: 25px;
            line-height: 13px;
            text-align: left;
            margin: 10px auto;
            @include text-ellipsis(2);
            white-space: normal;
          }
          > b {
            display: block;
            margin-left: 10%;
            font-size: 13px;
            color: #0d96da;
          }
        }
      }
    }

  }

  .class_title {
    margin-top: 18px;
    text-align: left;
    padding-left: 12px;
    > span {
      font-size: 27px;
      font-weight: bold;
    }
  }

  .fade-enter-active /* .fade-leave-active below version 2.1.8 */
  {
    opacity: 0;
  }

  .fade-enter-to /* .fade-leave-active below version 2.1.8 */
  {
    opacity: 1;
    transition: all .5s;
  }

  .search_wrap {
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 5;
    background-color: #25abea;
    padding-bottom: 6px;
    padding-top: 8px;
    max-width: 768px;
    min-width: 320px;
    > span {
      float: left;
      font-size: 13px;
      margin-top: 10px;
      margin-left: 10px;
      font-weight: bold;
      margin-right: 8px;
      letter-spacing: 2px;
      color: #eee;
    }
  }

  .search {
    overflow: hidden;
    position: relative;
    margin-right: 14px;
    height: 32px;
    > form {
      background-color: #93d4f4;
      padding-top: 7px;
      border-radius: 10px;
      padding-left: 10px;
      height: 32px;
      padding-right: 44px;
      input::-webkit-input-placeholder {
        color: #aaa;
      }
      input::-moz-placeholder {
        color: #aaa;
      }
      input:-moz-placeholder {
        color: #aaa;
      }
      input:-ms-input-placeholder {
        color: #aaa;
      }
      input[type=search]::-webkit-search-cancel-button {
        -webkit-appearance: none; //此处只是去掉默认的小×
      }
      input {
        width: 100%;
        height: 18px;
        line-height: 18px;
        outline: none;
        border: none;
        color: #fff;
        font-size: 14px;
        letter-spacing: 1px;
        background-color: transparent;
      }
    }
    > b {
      position: absolute;
      top: 0;
      right: 9px;
      width: 32px;
      height: 32px;
      background-image: url("../assets/img/22.png");
      background-size: 80% 80%;
      background-position-y: 42%;
      background-repeat: no-repeat;
      transition: transform 0.12s;
      cursor: pointer;
      &:active {

        transform: scale(1.2);
      }
    }
  }
</style>
